﻿#include "AppraisalBasis.h"
#include "DatabaseMgmt.h"

CAppraisalBasis::CAppraisalBasis()
{

}

CAppraisalBasis::~CAppraisalBasis()
{

}

int CAppraisalBasis::insertRow(QString law)
{
	int ret = 0;

	QString strSql = QString("INSERT INTO TB_APPRAISAL_BASIS (NAME) VALUES ('%1')")
		.arg(law);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}

	return ret;
}

int CAppraisalBasis::removeRow(int id)
{
	int ret = 0;

	QString strSql = QString("DELETE FROM TB_APPRAISAL_BASIS WHERE UID = %1")
		.arg(id);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}

	return ret;
}

int CAppraisalBasis::modifyRow(int id, QString law)
{
	int ret = 0;

	QString strSql = QString("UPDATE TB_APPRAISAL_BASIS SET NAME='%1' WHERE UID = %2")
		.arg(law)
		.arg(id);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}

	return ret;
}

int CAppraisalBasis::queryAll(QVector<LawBasis>& lawes)
{
	int ret = 0;

	QString strSql = QString("SELECT UID, NAME FROM TB_APPRAISAL_BASIS");

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}
	else
	{
		while (query.next())
		{
			LawBasis law;

			law.uid = query.value("UID").toUInt();
			law.law = query.value("NAME").toString();

			lawes.append(law);
		}

	}
	return ret;
}
